package org.kfive.nova;

import org.springframework.batch.core.*;
import org.springframework.batch.core.job.SimpleJob;
import org.springframework.batch.core.launch.support.SimpleJobLauncher;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.batch.core.repository.dao.*;
import org.springframework.batch.core.repository.support.SimpleJobRepository;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.core.task.TaskExecutor;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        //初始化Repository
        JobInstanceDao jobInstanceDao = new MapJobInstanceDao();
        JobExecutionDao jobExecutionDao = new MapJobExecutionDao();
        StepExecutionDao stepExecutionDao = new MapStepExecutionDao();
        ExecutionContextDao executionContextDao = new MapExecutionContextDao();
        JobRepository jobRepository = new SimpleJobRepository(jobInstanceDao,jobExecutionDao,stepExecutionDao,executionContextDao);
        //生成jobexecution
        Map<String,JobParameter> parameterMap = new LinkedHashMap<String, JobParameter>();
        parameterMap.put("id",new JobParameter(1L,false));
        parameterMap.put("name",new JobParameter("test",false));
        JobParameters parameters = new JobParameters(parameterMap);
        JobExecution execution = null;
        SimpleJobLauncher jobLauncher = new SimpleJobLauncher();

        try {
//            execution = jobRepository.createJobExecution("test",parameters);
            jobLauncher.setJobRepository(jobRepository);
            TaskExecutor taskExecutor = new SimpleAsyncTaskExecutor();
            jobLauncher.setTaskExecutor(taskExecutor);
            SimpleJob job = new SimpleJob("test");
            Step step = new Step() {
                public String getName() {
                    return "test-1";
                }

                public boolean isAllowStartIfComplete() {
                    return false;
                }

                public int getStartLimit() {
                    return 1;
                }

                public void execute(StepExecution stepExecution) throws JobInterruptedException {
                    System.out.println(getName()+":step is running"+stepExecution.getJobParameters().toString());

                }
            };
            job.addStep(step);
            job.setJobRepository(jobRepository);
            execution = jobLauncher.run(job,parameters);
            System.out.println(execution.toString());

        } catch (JobExecutionAlreadyRunningException e) {
            e.printStackTrace();
        } catch (JobRestartException e) {
            e.printStackTrace();
        } catch (JobInstanceAlreadyCompleteException e) {
            e.printStackTrace();
        } catch (JobParametersInvalidException e) {
            e.printStackTrace();
        } finally {

        }
    }
}
